package com.decoration.basic.controller.api;

import com.alibaba.druid.util.StringUtils;
import com.decoration.basic.entity.DecorationUser;
import com.decoration.basic.entity.FebackPo;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.service.DecFeedbackService;
import com.decoration.basic.service.DecUserService;
import com.decoration.basic.util.Global;
import com.decoration.basic.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class FeedbackController {
    private static Logger log = LoggerFactory.getLogger(DecDailyController.class);

    @Resource
    DecFeedbackService decFeedbackService;
    @Resource
    DecUserService decUserService;

    @RequestMapping(value ="/fedback/list", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getFedBacklist(JsonResult jsonResult, @RequestBody FebackPo inMsg, HttpServletRequest request){
        if (StringUtils.isEmpty(inMsg.getCtcid())){
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("输入参数为空，无法查询反馈信息");
            return jsonResult;
        }
        List<FebackPo> list = null;
        try {
            list = decFeedbackService.searchList(inMsg);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("查询异常，请联系管理员");
            return jsonResult;
        }
        if (list == null || list.size() <1){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setMsg("查询成功，反馈信息为空");
            return jsonResult;
        }
        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        jsonResult.setCount(list.size());
        jsonResult.setMsg("查询成功");
        jsonResult.setData(list);
        return jsonResult;
    }

    @RequestMapping(value ="/fedback/fedbackid", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getFedBackPo(JsonResult jsonResult, @RequestBody FebackPo inMsg, HttpServletRequest request){

        if (StringUtils.isEmpty(inMsg.getFebackid())){
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("输入参数为空，无法查询反馈信息");
            return jsonResult;
        }

        FebackPo list = null;
        try {
            list = decFeedbackService.searchFedbackPo(inMsg);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("查询异常，请联系管理员");
            return jsonResult;
        }
        if (list == null ){
            jsonResult.setCode(JsonResult.SUCCESS_CODE);
            jsonResult.setMsg("查询成功，反馈信息为空");
            return jsonResult;
        }

        jsonResult.setCode(JsonResult.SUCCESS_CODE);
        jsonResult.setCount(1);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(list);
        return jsonResult;
    }

    @RequestMapping(value ="/fedback/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addFedBackPo(JsonResult jsonResult, @RequestBody FebackPo inMsg, HttpServletRequest request){

        String userid = null;
        try {
            userid = (String)request.getSession().getAttribute(Global.SESSION_USER_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (userid == null){
            userid = "8f65f463e4e24a08af343106a79dab92";//测试专用
        }
        DecorationUser userinfo = decUserService.findById(userid);
        if (userinfo == null || userinfo.getPerType() != 3 ) {
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("当前用户未登录/无权限新增反馈");
            return jsonResult;
        }
        inMsg.setUserId(userinfo.getUserId());
        inMsg.setUserName(userinfo.getPerName());
        try {
            jsonResult =decFeedbackService.insert(inMsg);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("新增异常，请联系管理员");
            return jsonResult;
        }

        return jsonResult;
    }

    @RequestMapping(value ="/fedback/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateFedBackPo(JsonResult jsonResult, @RequestBody FebackPo inMsg, HttpServletRequest request){

        try {
            jsonResult = decFeedbackService.update(inMsg);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("更新异常，请联系管理员");
            return jsonResult;
        }
        return jsonResult;
    }

    @RequestMapping(value ="/fedback/del", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delFedBackPo(JsonResult jsonResult, @RequestBody FebackPo inMsg, HttpServletRequest request){

        try {
            jsonResult = decFeedbackService.delete(inMsg);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(JsonResult.FAILED_CODE);
            jsonResult.setMsg("删除异常，请联系管理员");
            return jsonResult;
        }
        return jsonResult;
    }
}
